package com.yinhai.message.implement.sms.sender;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.yinhai.core.common.api.config.impl.SysConfig;
import com.yinhai.message.api.bean.TMMessage;
import com.yinhai.message.api.bean.TMUser;
import com.yinhai.message.api.exception.TMException;
import com.yinhai.message.api.vo.TMMessageVo;
import com.yinhai.message.core.sender.IMessageSender;
import com.yinhai.message.core.util.IMessageConstant;
/**
 * 
 * @author yanglq
 * 2017.6.19
 */
@Service
public class SmsMessageSender implements IMessageSender<TMMessageVo>{

	private String username = SysConfig.getSysConfig("smsUsername","yh");
	private String password = SysConfig.getSysConfig("smsPassword","yfZhGAGj");

	@Override
	public List<TMUser> sendMessage(TMMessageVo vo) {
		List<TMUser> receives = vo.getUserlist();
		if("true".endsWith(SysConfig.getSysConfig("smsIsOpen","false"))){
//			TMMessage msg = vo.getMessage();
			List<String> phoneList =new ArrayList<>();
			for (TMUser receive : receives) {
				phoneList.add(receive.getSms());
			}
			String[] responseText ={"no","没有获取到短信接口返回信息"};
			try {
//				AeyeSendSMS aeyeSendSMS = AeyeSendSMS.getInstance();
//				responseText = aeyeSendSMS.postSMS(username,password,phoneList,msg.getContent());
//				responseText = DYUtils.sendSms(phoneList, msg.getContent());
			} catch (Exception e) {
				throw new TMException(e);
			}
			if("0".equals(responseText[0])){
				for (TMUser receive : receives) {
					receive.setSendSign(IMessageConstant.MSG_STATE_SEND_ALREADY);
				}
			}else{
				for (TMUser receive : receives) {
					receive.setSendSign(IMessageConstant.MSG_STATE_SEND_FAIL);
					receive.setErrorInfo(responseText[1]);
				}
			}
		}else{
			for (TMUser receive : receives) {
				receive.setSendSign(IMessageConstant.MSG_STATE_SEND_FAIL);
				receive.setErrorInfo("短信功能关闭中");
			}
		}
		return receives;
	}

}
